# encoding:utf8
'''2017-7-8 pylint-format'''
import logging
import dbmgr
import redismgr

class IncDB(object):
    '''数据库单例'''
    _shareMongodb = None
    _shareRedis = None

    _isinit = False

    _cfgmongo = None
    _cfgredis = None

    @classmethod
    def init(cls, dbname, host, port, **kwads):
        '''使用参数初始化数据库'''
        if dbname == "mongo":
            cls._cfgmongo = dict(host=host, port=port, **kwads)
        elif dbname == "redis":
            cls._cfgredis = dict(host=host, port=port, **kwads)
        logging.info("ShareDb manager set %s init!", dbname)

    @classmethod
    def inc_db(cls):
        '''获取数据库'''

        if not cls._shareMongodb:
            cls._shareMongodb = dbmgr.DbMgr(
                cls._cfgmongo["host"], cls._cfgmongo["port"])
        return cls._shareMongodb

    @classmethod
    def inc_redis(cls):
        '''获取redis'''

        if not cls._shareRedis:
            print (cls._cfgredis, "--->>config")
            cls._shareRedis = redismgr.RedisMgr(
                cls._cfgredis["host"], cls._cfgredis["port"])
        return cls._shareRedis

#===================================
# Test db Driver
#===================================
# inc.shareMongodb().use("kankkindle")
# colleection = inc.shareMongodb().use_collection("books")
# print (colleection.count())
# print (colleection.database)
# ret = inc.shareMongodb().db().find_one({"format":"mobi"})
# print (ret["_id"])
# print (ret)


#===================================
# Test redis Driver
#===================================
# cli = inc.shareRedis().redis()
# print (cli.keys())
